# syntax = docker/dockerfile:1.4
# vim: syntax=dockerfile

ARG DNF_OPTS="\
  --enablerepo crb \
  --nobest \
  --nodocs \
  --setopt=install_weak_deps=0 \
  --setopt=keepcache=1 \
  --setopt=cachedir=/var/cache/dnf \
  "

FROM quay.io/centos/centos:stream9 AS build

ARG SPDK_CEPH_VERSION \
    SPDK_VERSION \
    DNF_OPTS \
    SPDK_PKGDEP_ARGS \
    SPDK_CONFIGURE_ARGS \
    SPDK_TARGET_ARCH \
    SPDK_MAKEFLAGS

RUN <<EOF
    echo Log variables
    echo ======================================================================
    echo SPDK_PKGDEP_ARGS=$SPDK_PKGDEP_ARGS
    echo SPDK_CONFIGURE_ARGS=$SPDK_CONFIGURE_ARGS
    echo SPDK_TARGET_ARCH=$SPDK_TARGET_ARCH
    echo SPDK_MAKEFLAGS=$SPDK_MAKEFLAGS
    echo ======================================================================
EOF

WORKDIR /src
COPY . .

COPY <<EOF /etc/yum.conf
[main]
gpgcheck=1
installonly_limit=3
clean_requirements_on_remove=True
best=False
skip_if_unavailable=False
cachedir=/var/cache/dnf
install_weak_deps=0
keepcache=True
tsflags=nodocs
EOF

COPY <<EOF /etc/yum.repos.d/ceph.repo
[Ceph]
name=Ceph packages for \$basearch
baseurl=https://download.ceph.com/rpm-${SPDK_CEPH_VERSION:?}/el\$releasever/\$basearch
enabled=1
priority=2
gpgcheck=1
gpgkey=https://download.ceph.com/keys/release.asc

[Ceph-noarch]
name=Ceph noarch packages
baseurl=https://download.ceph.com/rpm-$SPDK_CEPH_VERSION/el\$releasever/noarch
enabled=1
priority=2
gpgcheck=1
gpgkey=https://download.ceph.com/keys/release.asc
EOF

RUN \
    --mount=type=cache,target=/var/cache/dnf \
    --mount=type=cache,target=/var/lib/dnf \
    --mount=type=cache,target=/root/.cache/pip \
    dnf install $DNF_OPTS -y 'dnf-command(config-manager)' \
    && dnf config-manager --set-enabled crb \
    && dnf install $DNF_OPTS -y \
        rpm-build \
        git-core \
    && rpm -vih https://buildlogs.centos.org/centos/9-stream/storage/x86_64/ceph-reef/Packages/t/thrift-0.15.0-3.el9s.x86_64.rpm \
    && scripts/pkgdep.sh $SPDK_PKGDEP_ARGS \
    && dnf $DNF_OPTS update -y

RUN \
    --mount=type=cache,target=/var/cache/dnf \
    --mount=type=cache,target=/var/lib/dnf \
    DEPS="no" \
    SPDK_VERSION=${SPDK_VERSION:?} \
    RPM_RELEASE=0 \
    MAKEFLAGS=$SPDK_MAKEFLAGS \
    rpmbuild/rpm.sh $SPDK_CONFIGURE_ARGS --target-arch="${SPDK_TARGET_ARCH:?}"

# build bdevperf example, will not be a part of generated rpm
RUN make -C ./examples/bdev/bdevperf

#------------------------------------------------------------------------------
FROM registry.access.redhat.com/ubi9/ubi AS rpm-export
COPY --from=build /root/rpmbuild/rpm /rpm

#------------------------------------------------------------------------------
FROM registry.access.redhat.com/ubi9/ubi as spdk

ARG SPDK_CEPH_VERSION \
    SPDK_VERSION \
    DNF_OPTS

ARG SPDK_CENTOS_BASE="https://mirror.stream.centos.org/9-stream/BaseOS/x86_64/os/Packages/"
# This would become obsolete as the release rolls out new packages
ARG SPDK_CENTOS_REPO_VER="9.0-21.el9"

ARG SPDK_PKGDEP_ARGS \
    SPDK_CONFIGURE_ARGS \
    SPDK_MAKEFLAGS

ARG SPDK_NAME \
    SPDK_SUMMARY \
    SPDK_DESCRIPTION \
    SPDK_URL \
    SPDK_MAINTAINER \
    BUILD_DATE \
    SPDK_GIT_REPO \
    SPDK_GIT_BRANCH \
    SPDK_GIT_COMMIT

# Generic labels
LABEL name="$SPDK_NAME" \
      version="$SPDK_VERSION" \
      summary="$SPDK_SUMMARY" \
      description="$SPDK_DESCRIPTION" \
      maintainer="$SPDK_MAINTAINER" \
      release="" \
      url="$SPDK_URL" \
      build-date="$BUILD_DATE" \
      vcs-ref="$SPDK_GIT_COMMIT"

# SPDK-specific labels
LABEL io.spdk.version="$SPDK_VERSION" \
      io.spdk.build.pkgdep-args="$SPDK_PKGDEP_ARGS" \
      io.spdk.build.configure-args="$SPDK_CONFIGURE_ARGS" \
      io.spdk.build.makeflags="$SPDK_MAKEFLAGS" \
      io.spdk.build.target-arch="$SPDK_TARGET_ARCH" \
      io.spdk.build.ceph-release="$SPDK_CEPH_VERSION" \
      io.spdk.git.repo="$SPDK_GIT_REPO" \
      io.spdk.git.branch="$SPDK_GIT_BRANCH" \
      io.spdk.git.commit="$SPDK_GIT_COMMIT"

# CentOS-specific labels
LABEL org.centos.url="$SPDK_CENTOS_BASE" \
      org.centos.version="$SPDK_CENTOS_REPO_VER"

COPY --from=build /etc/yum.conf /etc/yum.conf
COPY --from=build /etc/yum.repos.d/ceph.repo /etc/yum.repos.d/ceph.repo

RUN \
    --mount=type=bind,from=build,source=/root/rpmbuild/rpm,target=/rpm \
    --mount=type=cache,target=/var/cache/dnf \
    --mount=type=cache,target=/var/lib/dnf \
    rpm -vih $SPDK_CENTOS_BASE/centos-stream-repos-$SPDK_CENTOS_REPO_VER.noarch.rpm \
             $SPDK_CENTOS_BASE/centos-gpg-keys-$SPDK_CENTOS_REPO_VER.noarch.rpm \
    && rpm -vih https://buildlogs.centos.org/centos/9-stream/storage/x86_64/ceph-reef/Packages/t/thrift-0.15.0-3.el9s.x86_64.rpm \
    && dnf $DNF_OPTS install -y /rpm/$(uname -m)/*.rpm \
    && dnf $DNF_OPTS update -y

ENTRYPOINT [ "/usr/local/bin/nvmf_tgt" ]
CMD [ "-u", "-r", "/var/tmp/spdk.sock" ]

#------------------------------------------------------------------------------
FROM spdk AS bdevperf

# Default test run duration in secs
ENV BDEVPERF_TEST_DURATION="90"

# RPC socket path
ENV BDEVPERF_SOCKET="/tmp/bdevperf.sock"

# IO Queue depth
ENV BDEVPERF_IO_QUEUE="128"

# IO Block size
ENV BDEVPERF_IO_BS="4096"

# Type of I/O pattern, see https://spdk.io/doc/bdevperf.html
# for available types
ENV BDEVPERF_IO_TYPE="verify"

COPY --from=build /src/build/examples/bdevperf /usr/local/bin/bdevperf
COPY --from=build /src/examples/bdev/bdevperf/bdevperf.py /usr/libexec/spdk/scripts/bdevperf.py
RUN echo "#!/bin/sh -x"                                                 > /entrypoint
RUN echo "bdevperf -z -r \$BDEVPERF_SOCKET \\"                         >> /entrypoint
RUN echo "         -q \$BDEVPERF_IO_QUEUE -o \$BDEVPERF_IO_BS \\"      >> /entrypoint
RUN echo "         -w \$BDEVPERF_IO_TYPE -t \$BDEVPERF_TEST_DURATION"  >> /entrypoint
RUN chmod 755 /entrypoint

ENTRYPOINT [ "/entrypoint" ]
